package shun.demo.gson;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import shun.demo.gson.model.FeaturedInfo;
import shun.demo.gson.model.Game;
import shun.demo.gson.model.Result;

import java.lang.reflect.Type;

/**
 * Created with IntelliJ IDEA.
 * User: chen
 * Date: 11/19/13
 * Time: 12:31 PM
 * To change this template use File | Settings | File Templates.
 */
public class Main {

    private Gson mGson = new Gson();

    public static void main(String[] args) {
        Main main = new Main();
//        main.testGame();

//        main.testResult();

//        main.testResultList();
        main.testFeaturedInfo();
    }

    public void testGame() {
        Game game = new Game("植物大战僵尸", "http://www.baidu.com/icon.jgp", "123", "com.baidu.www");
        String json = mGson.toJson(game);
        System.out.println(json);

        Game game1 = mGson.fromJson(json, Game.class);
        System.out.println(game1.toString());
        System.out.println(mGson.toJson(game1.getState()));
    }

    public void testResult() {
        Result<Game> result = new Result<Game>();
        result.setStatus(0);
        result.setMessage("成功");
        Game game = new Game("植物大战僵尸", "http://www.baidu.com/icon.jgp", "123", "com.baidu.www");
        result.setContent(game);

        Type type = new TypeToken<Result<Game>>(){}.getType();
        String json = mGson.toJson(result, type);

        System.out.println(json);

        Result<Game> result1 = mGson.fromJson(json, type);
        Game game1 = result1.getContent();
        System.out.println(game1.toString());
    }

    public void testResultList() {

        System.out.println("test list");

        Result<Game[]> result = new Result<Game[]>();
        result.setStatus(0);
        result.setMessage("成功");
        Game game = new Game("植物大战僵尸", "http://www.baidu.com/icon.jgp", "123", "com.baidu.www");
        Game game1 = new Game("疯狂的赛车", "http://www.fksc.com/icon.jpg", "122", "com.fksc.www");
        Game[] games = new Game[] {
                game,
                game1,
        };

        result.setContent(games);

        Type type = new TypeToken<Result<Game[]>>(){}.getType();
        String json = mGson.toJson(result, type);

        System.out.println(json);

        Result<Game[]> result1 = mGson.fromJson(json, type);
        Game[] games1 = result1.getContent();
        for (int i = 0; i < games1.length; i++) {
            System.out.println(games1[i]);
        }
    }

    private void testFeaturedInfo() {
        String json =
                "{\n" +
                "    \"status\":0,\n" +
                "    \"msg\":\"\\u6570\\u636e\\u52a0\\u8f7d\\u6210\\u529f\\uff01\",\n" +
                "    \"result\":\n" +
                "    {\n" +
                "        \"time\":1378363614,\n" +
                "        \"head\":                                          \n" +
                "        [\n" +
                "            {\n" +
                "                \"pic_url\":\"http://www.baidu.com/icon.jgp\",\n" +
                "                \"type\":\"0\",                              \n" +
                "                \"object\":\n" +
                "                {\n" +
                "                    \"game_id\":\"1111\",       \n" +
                "                    \"game_name\":\"游戏名\",    \n" +
                "                    \"pack_name\":\"com.baidu.www\", \n" +
                "                    \"icon_url\":\"http://www.baidu.com/icon.jgp\"\n" +
                "                }\n" +
                "            },\n" +
                "            {\n" +
                "                \"pic_url\":\"http://www.baidu.com/icon.jgp\",\n" +
                "                \"type\":\"1\",\n" +
                "                \"object\":\n" +
                "                {\n" +
                "                    \"union_id\":\"11\",\n" +
                "                    \"union_name\":\"好合集\"\n" +
                "                }\n" +
                "            }\n" +
                "        ],\n" +
                "        \"recommend\":\n" +
                "        [\n" +
                "            {\n" +
                "                \"pic_url\":\"http://www.baidu.com/icon.jgp\",\n" +
                "                \"type\":\"1\",\n" +
                "                \"object\":\n" +
                "                {\n" +
                "                    \"union_id\":\"11\",\n" +
                "                    \"union_name\":\"好合集\"\n" +
                "                }\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";

        Type type = new TypeToken<Result<FeaturedInfo>>(){}.getType();

        System.out.println(type);

        Result<FeaturedInfo> result = mGson.fromJson(json, type);

        System.out.println(result);

    }


}
